package com.gzhryc.net_marketing.cloud.logistics;

import java.util.ArrayList;
import java.util.List;

import com.gzhryc.common.api.DefaultAPICodeConstants;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.api.ConfigAPIService;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsCostListRequest;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsCostListResponse;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsUsableCostRequest;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsUsableCostResponse;
import com.gzhryc.net_marketing.core.models.LogisticsCostItem;
import com.gzhryc.shared_device.base.ILogisticsCostConfig;

public class LogisticsCostUtils {

    static Logger log = Logger.getLogger(LogisticsCostUtils.class);

    public static ILogisticsCostConfig getUsableConfig(Long supplierId,String logisticsCompanyId,String province) throws LogicException {
        if (supplierId != null) {
            LogisticsUsableCostRequest apiReq = new LogisticsUsableCostRequest();
            apiReq.getBody().setSupplyId(supplierId);
            apiReq.getBody().setCompanyId(logisticsCompanyId);
            apiReq.getBody().setProvince(province);

            LogisticsUsableCostResponse apiResp = ConfigAPIService.self().getLogisticsUsableCostList(apiReq);
            if (apiResp != null && DefaultAPICodeConstants.SUCCESS.equals(apiResp.getCode())) {
                LogisticsCostItem costItem = apiResp.getBody();

                DefLogisticsCostConfig costConfig = new DefLogisticsCostConfig();
                costConfig.setCompanyId(costItem.getCompanyId());
                costConfig.setCompanyName(costItem.getCompanyName());
                costConfig.setAreaScope(costItem.getAreaScope());
                costConfig.setCountry(costItem.getCountry());
                costConfig.setProvince(costItem.getProvince());
                costConfig.setMoney(costItem.getMoney());
                costConfig.setFreeMinMoney(costItem.getFreeMinMoney());

                return costConfig;
            }
        }
        return null;
    }

    public static List<ILogisticsCostConfig> findCostConfig(Long supplierId,String province) {
        if (supplierId != null) {
            LogisticsCostListRequest apiReq = new LogisticsCostListRequest();
            apiReq.getBody().setSupplyId(supplierId);
            apiReq.getBody().setProvince(province);

            LogisticsCostListResponse apiResp = ConfigAPIService.self().findLogisticsCostList(apiReq);
            log.info("返回：{{0:json}}",apiResp);
            if (apiResp != null && DefaultAPICodeConstants.SUCCESS.equals(apiResp.getCode())) {
                if (apiResp.getBody().getItems() != null && apiResp.getBody().getItems().size() > 0) {
                    List<ILogisticsCostConfig> result = new ArrayList<>();
                    for (LogisticsCostItem costItem : apiResp.getBody().getItems()) {
                    	DefLogisticsCostConfig costConfig = new DefLogisticsCostConfig();
                        costConfig.setCompanyId(costItem.getCompanyId());
                        costConfig.setCompanyName(costItem.getCompanyName());
                        costConfig.setAreaScope(costItem.getAreaScope());
                        costConfig.setCountry(costItem.getCountry());
                        costConfig.setProvince(costItem.getProvince());
                        costConfig.setMoney(costItem.getMoney());
                        costConfig.setFreeMinMoney(costItem.getFreeMinMoney());
                        result.add(costConfig);
                    }
                    return result;
                }
            }
        }
        return null;
    }
    
    public static boolean isFree(ILogisticsCostConfig costConfig,Integer money){
        return costConfig.getFreeMinMoney() == null || (costConfig.getFreeMinMoney() > 0 && money >= costConfig.getFreeMinMoney());
    }

}
